package cn.stylefeng.guns.modular.rest;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.usr.entity.Favor;
import cn.stylefeng.guns.modular.usr.model.params.FavorParam;
import cn.stylefeng.guns.modular.usr.model.result.FavorResult;
import cn.stylefeng.guns.modular.usr.service.FavorService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


/**
 * 新闻收藏控制器
 *
 * @author
 * @Date 2019-12-17 17:27:48
 */
@RestController
@RequestMapping("/miniprogramApi/favor")
public class ApiFavorController extends BaseController {
    private final FavorService favorService;

    public ApiFavorController(FavorService favorService) {
        this.favorService = favorService;
    }

    @RequestMapping(value = "/list")
    public Object List(String openId) {
        SuccessResponseData SUCCESS_TIP = new SuccessResponseData();
        FavorParam param = new FavorParam();
        param.setOpenId(openId);
        List<FavorResult> favorResultList = favorService.findListBySpec(param);
        favorResultList.forEach(newsResult -> {
            String contentText = "";
            if (newsResult.getContentText() != null && newsResult.getContentText().replaceAll("\n", "").replaceAll("　", "").replaceAll(" ", "").trim().length() > 20) {
                contentText = newsResult.getContentText().replaceAll("\n", "").replaceAll("　", "").replaceAll(" ", "").trim().substring(0, 20);
            } else {
                contentText = newsResult.getContentText().replaceAll("\n", "").replaceAll("　", "").replaceAll(" ", "").trim();
            }
            newsResult.setContentText(contentText);
        });
        SUCCESS_TIP.setData(favorResultList);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/add_or_delete")
    public Object favor(String openId, int newsId) {
        SuccessResponseData SUCCESS_TIP = new SuccessResponseData();

        FavorParam favorParam = new FavorParam();
        favorParam.setOpenId(openId);
        favorParam.setNewsId(newsId);
        FavorResult favorResult = favorService.findBySpec(favorParam);
        if (favorResult.getId() == null) {
            Favor favor = new Favor();
            favor.setNewsId(newsId);
            favor.setOpenId(openId);
            favor.setCreateTime(new Date());
            favorService.save(favor);
// 收藏成功
            SUCCESS_TIP.setData("add_ok");
        } else {
            favorService.removeById(favorResult.getId());
// 取消收藏
            SUCCESS_TIP.setData("delete_ok");
        }


        return SUCCESS_TIP;
    }

}


